package com.mactivity.activities;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.mactivity.database.NotificationActivityFunctions;
import com.mactivity.helper.Constants;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Author: Sanjay Vasudeva
 * Class gets details of a Friend request and handles Accepting and Rejecting friend request
 * */
public class NotificationFriendActivity extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork() // or	.detectAll() for all detectable problems
		.penaltyLog().build());

		super.onCreate(savedInstanceState);
		setContentView(R.layout.notificationpost);

		Button accept,reject,delete;
		int userid=0;
		accept = (Button) findViewById(R.id.buttonaccept);
		reject = (Button) findViewById(R.id.buttonreject);
		delete = (Button) findViewById(R.id.buttonDelete);
		TextView requestfromname = (TextView) findViewById(R.id.requestfromname);        
		TextView requestfromemail = (TextView) findViewById(R.id.requestfromemail);        
		TextView groupplansport = (TextView) findViewById(R.id.groupplansport);        
		TextView groupplandate = (TextView) findViewById(R.id.groupplandate);        
		TextView groupplantime = (TextView) findViewById(R.id.groupplantime);        
		TextView groupplansportlabel = (TextView) findViewById(R.id.groupplansportlabel);        
		TextView groupplandatelabel = (TextView) findViewById(R.id.groupplandatelabel);        
		TextView groupplantimelabel = (TextView) findViewById(R.id.groupplantimelabel);        

		accept.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View arg0) {
				final int ACTIVITY_CREATE = 0;
				Bundle extras = getIntent().getExtras();
				int noteid,notefrom,noteto;
				noteid=extras.getInt(Constants.COLUMN_NOTIFICATION_NOTEID);
				notefrom=extras.getInt(Constants.CONSTANT_EXTRAS_USERID);
				noteto=extras.getInt(Constants.CONSTANT_EXTRAS_TOID);
				Integer N=new Integer(noteid);
				Integer F=new Integer(notefrom);
				Integer T=new Integer(noteto);
				Integer A=new Integer(2);
				try {
					NotificationActivityFunctions notificationActivityFunctions=new NotificationActivityFunctions();
					//Reflect in Notification that friend request has been accepted
					notificationActivityFunctions.acceptRejectFriendRequest(N, F, T, A);
					//Add in Friends table
					notificationActivityFunctions.addAsFriends(F, T);
					//On clicking of accept button,move to Notifications list
					Intent i = new Intent(NotificationFriendActivity.this, NotificationActivity.class);
					startActivityForResult(i, ACTIVITY_CREATE);
					NotificationFriendActivity.this.finish();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Log.e(Constants.LOGGER_ERROR_TAG,"Exception in FriendActivity onClick Accept"+ e.getMessage());
				}
			}

		});

		reject.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final int ACTIVITY_CREATE = 0;
				Bundle extras = getIntent().getExtras();
				int noteid,notefrom,noteto;
				noteid=extras.getInt(Constants.COLUMN_NOTIFICATION_NOTEID);
				notefrom=extras.getInt(Constants.CONSTANT_EXTRAS_USERID);
				noteto=extras.getInt(Constants.CONSTANT_EXTRAS_TOID);
				Integer N=new Integer(noteid);
				Integer F=new Integer(notefrom);
				Integer T=new Integer(noteto);
				Integer A=new Integer(3);
				try {
					//Reflect in Notification that friend request has been rejected
					NotificationActivityFunctions notificationActivityFunctions=new NotificationActivityFunctions();
					notificationActivityFunctions.acceptRejectFriendRequest(N, F, T, A);
					//On clicking of reject button,move to Notifications list
					Intent i = new Intent(NotificationFriendActivity.this, NotificationActivity.class);
					startActivityForResult(i, ACTIVITY_CREATE);
					NotificationFriendActivity.this.finish();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Log.e(Constants.LOGGER_ERROR_TAG,"Exception in FriendActivity onClick Reject"+ e.getMessage());
				}
			}
		});

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			userid = extras.getInt(Constants.CONSTANT_EXTRAS_USERID);
		}
		Integer I=new Integer(userid);
		String response = null;

		try{
			NotificationActivityFunctions notificationActivityFunctions=new NotificationActivityFunctions();
			response = notificationActivityFunctions.fetchUserDetails(I);
			try{
				JSONArray jArray = new JSONArray(response);
				for(int i=0;i<jArray.length();i++){
					JSONObject json_data = jArray.getJSONObject(i);
					String noteFrom=json_data.getString(Constants.JSON_STRING_NAME);
					String noteEmail=json_data.getString(Constants.JSON_STRING_EMAIL);
					//show Name and email and hide the OK button
					requestfromname.setText(noteFrom);
					requestfromemail.setText(noteEmail);        
					groupplansport.setVisibility(View.INVISIBLE) ;        
					groupplandate.setVisibility(View.INVISIBLE) ;        
					groupplantime.setVisibility(View.INVISIBLE) ;        
					groupplansportlabel.setVisibility(View.INVISIBLE);        
					groupplandatelabel.setVisibility(View.INVISIBLE);        
					groupplantimelabel.setVisibility(View.INVISIBLE);    
					delete.setVisibility(View.INVISIBLE);
				}
			}
			catch(JSONException e){
				Log.e("log_tag", "Error parsing data "+e.toString());
			}
		}
		catch(Exception e){

		}

	}
	@Override
    public void onBackPressed() {
		final int ACTIVITY_CREATE = 0;
		Intent i = new Intent(NotificationFriendActivity.this, NotificationActivity.class);
		startActivityForResult(i, ACTIVITY_CREATE);
		NotificationFriendActivity.this.finish();
    }
}

